package com.td.erhome.adapter;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.ActivityOptionsCompat;
import android.support.v7.widget.RecyclerView;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.td.erhome.R;
import com.td.erhome.models.CollectBean;
import com.td.erhome.models.HouseBean;
import com.td.erhome.ui.bughouse.HouseInfoActivity;
import com.td.erhome.utils.DisplayUtil;
import com.td.erhome.utils.ImageLoader;
import com.td.erhome.utils.RxBus;
import com.td.erhome.utils.SpannableStringUtils;

import java.util.List;

import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;


/**
 * Created by Administrator on 2017/4/18 0018.
 */

public class BugHouseListAdapter extends RecyclerView.Adapter<BugHouseListAdapter.HouseHolder> {

    private  DisplayMetrics displayMetrics;
    private  float density;
    public  Subscription mSubscribe;
    private Activity mContext;
    public List<HouseBean.SellHouseListBean> mhouse_list;
    private int mPoint;

    public BugHouseListAdapter(Activity context,List<HouseBean.SellHouseListBean> house_list) {
        mContext = context;
        this.mhouse_list = house_list;

        //获取当前手机屏幕的像素密度比例
        displayMetrics = context.getResources().getDisplayMetrics();
        density = displayMetrics.density;



        //收藏 取消收藏涮新
        mSubscribe = RxBus.getInstance().toObserverable(CollectBean.class)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<CollectBean>() {
                    @Override
                    public void call(CollectBean collectBean) {
                       mhouse_list.get(mPoint).is_favorite =  collectBean.isCollect+"";
                    }
                });

    }

    public void addData(List<HouseBean.SellHouseListBean> house_list){
        mhouse_list.addAll(house_list);
        String s = mhouse_list.toString();
        notifyDataSetChanged();
    }

    public void clearData(){
        mhouse_list.clear();
        notifyDataSetChanged();
    }

    @Override
    public HouseHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View  inflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycle_house_list_item, null);
        HouseHolder cardHolder = new HouseHolder(inflate);
        return  cardHolder;
    }
    @Override
    public void onBindViewHolder(final HouseHolder holder, final int position) {
        ImageLoader.loadImage(mContext,mhouse_list.get(position).house_image_list.size()==0?"":mhouse_list.get(position).house_image_list.get(0).house_image,R.mipmap.td_moreng,holder.imgBanner);

        holder.mTvAddress.setText(SpannableStringUtils
                .getBuilder("").append(mhouse_list.get(position).district_name).setProportion(15).append("  "+mhouse_list.get(position).area_name).setProportion(12).create());

        holder.tvAcrege.setText(mhouse_list.get(position).type_of_rooms+"室"+mhouse_list.get(position).type_of_hall+"厅"+mhouse_list.get(position).type_of_guards+"卫    "+mhouse_list.get(position).house_dimensions+"㎡");

        holder.tvMoney.setText(SpannableStringUtils.getBuilder("").append(mhouse_list.get(position)
                .sell_amount+"万\n").setProportion(15)
                 .setForegroundColor(mContext.getResources().getColor(R.color.color_f70707))
                 .append(mhouse_list.get(position).average_price+"元/㎡").setProportion(10)
                .setForegroundColor(mContext.getResources().getColor(R.color.color_bdbdbd)).create());

        List<String> house_tag = mhouse_list.get(position).house_tag;

        holder.linAddView.removeAllViews();

        addFacilitiesImg(holder.linAddView,house_tag);


        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                    mPoint = position;
                    ActivityOptionsCompat options =
                            ActivityOptionsCompat.makeSceneTransitionAnimation(mContext,
                                    holder.imgBanner, mContext.getString(R.string.transition_news_img));
                    Intent intent = new Intent();
                    intent.setClass(mContext, HouseInfoActivity.class);
                    Bundle bundle = new Bundle();
                    bundle.putSerializable("SellHouseListBean", mhouse_list.get(position));
                    intent.putExtras(bundle);
                    ActivityCompat.startActivity(mContext,intent,options.toBundle());
            }
        });

    }

    @Override
    public int getItemCount() {
        return mhouse_list.size();
    }



    private void addFacilitiesImg(LinearLayout linearLayout , List<String> house_tag) {
        for (int i=0 ; i<house_tag.size() ; i++){
            String s = house_tag.get(i);
            TextView textView = new TextView(mContext);

            LinearLayout.LayoutParams layoutParams =
                    new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            //设置居中显示：
            layoutParams.gravity = Gravity.CENTER;
            //设置它的上下左右的margin
            layoutParams.setMargins((int)(4* density) , 0 , (int)(4* density) , 0);

            textView.setLayoutParams(layoutParams);
            textView.setTextColor(mContext.getResources().getColor(R.color.color_048d99));
            textView.setBackgroundResource(R.drawable.shape_status);
            textView.setPadding((int)(5* density) , (int)(3*density) , (int)(5* density) , (int)(3*density));
            textView.setText(s);
            textView.setTextSize(DisplayUtil.px2sp(mContext,34));
            linearLayout.addView(textView);
        }

     //   linearLayout.setPadding(0 , (int)(10* density) , 0 , (int)(10* density));
    }




    public class HouseHolder extends RecyclerView.ViewHolder {

        private final ImageView imgBanner;
        private final TextView mTvAddress;
        private final TextView tvAcrege;
        private final TextView tvMoney;
        private final LinearLayout linAddView;

        public HouseHolder(View itemView) {
            super(itemView);
            imgBanner = (ImageView)itemView.findViewById(R.id.img_banner);
            mTvAddress = (TextView) itemView.findViewById(R.id.tv_address);
            tvAcrege = (TextView) itemView.findViewById(R.id.tv_acreage);
            tvMoney = (TextView) itemView.findViewById(R.id.tv_money);
            linAddView = (LinearLayout) itemView.findViewById(R.id.lin_add_view);
        }
    }
}
